﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.OA;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.OA
{
    public class EmployeesServices:IEmployees
    {
        public IBaseRepository Db { get; }

        public EmployeesServices(IBaseRepository db)
        {
            Db = db;
        }

        public async Task<IEnumerable<Employees>> GetAllAsync()
        {
            string sql = "select * from Employees";
            var list = await Db.QueryAllAsync<Employees>(sql);
            return list;
        }
        public async Task<IEnumerable<Employees>> GetAsync(int PageIndex, int PageSize, string PersonCode, string Name, string IdCard)
        {
            string sql = "select * from Employees where 1=1 ";

            if (!string.IsNullOrEmpty(PersonCode))
            {
                sql += " and PersonCode like @PersonCode ";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }
            if (!string.IsNullOrEmpty(IdCard))
            {
                sql += " and IdCard like @IdCard ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<Employees>(sql, new { PageIndex, PageSize, PersonCode = $"%{PersonCode}%", Name = $"%{Name}%", IdCard = $"%{IdCard}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string PersonCode, string Name, string IdCard)
        {
            string sql = "select count(*) from Employees where 1=1 ";


            if (!string.IsNullOrEmpty(PersonCode))
            {
                sql += " and PersonCode like @PersonCode ";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }

            if (!string.IsNullOrEmpty(IdCard))
            {
                sql += " and IdCard like @IdCard ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { PersonCode = $"%{PersonCode}%", Name = $"%{Name}%", IdCard = $"%{IdCard}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(Employees model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }

        public async Task<bool> AddAsync(Employees model)
        {

            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete Employees where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Employees where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

    }
}
